package com.dces.jx.billtest.xwManageHistoricalState.service;
import com.dces.jx.billtest.dto.xwManageHistoricalState.DcDecLogDto;
import com.dces.jx.billtest.dto.xwManageHistoricalState.DcDecLogParam;
import com.dces.jx.billtest.dto.xwManageHistoricalState.DcDecLogExportParam;
import com.dces.jx.billtest.xwManageHistoricalState.dao.DcDecLogMapper;
import com.dces.jx.billtest.xwManageHistoricalState.mapper.DcDecLogDtoMapper;
import com.dces.jx.billtest.xwManageHistoricalState.model.DcDecLog;
import org.apache.commons.collections4.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xdo.common.base.service.BaseService;
import com.xdo.common.token.UserInfoToken;
import com.xdo.domain.PageParam;
import com.xdo.domain.ResultObject;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.UUID;
import java.util.stream.Collectors;
/**
 * generated by Generate dcits
 * Service;
 *
 * @author xdoV2.0
 * @date: 2021-9-10
 */
@Service
public class DcDecLogService extends BaseService<DcDecLog> {
    @Resource
    private DcDecLogMapper dcDecLogMapper;
    @Resource
    private DcDecLogDtoMapper dcDecLogDtoMapper;
    @Override
    public Mapper<DcDecLog> getMapper() {
        return dcDecLogMapper;
    }
    /**
     * 功能描述: grid分页查询
     *
     * @param dcDecLogParam
     * @param pageParam
     * @return
     */
    public ResultObject<List<DcDecLogDto>> selectAllPaged(DcDecLogParam dcDecLogParam, PageParam pageParam) {
        // 启用分页查询
        DcDecLog dcDecLog = dcDecLogDtoMapper.toPo(dcDecLogParam);
        Page<DcDecLog> page = PageHelper.startPage(pageParam.getPage(), pageParam.getLimit(), pageParam.getSortOrderContent())
                .doSelectPage(() -> dcDecLogMapper.getList(dcDecLog));
        List<DcDecLogDto> dcDecLogDtos = page.getResult().stream().map(head -> {
            DcDecLogDto dto = dcDecLogDtoMapper.toDto(head);
            return dto;
        }).collect(Collectors.toList());
		 ResultObject<List<DcDecLogDto>> paged = ResultObject.createInstance(dcDecLogDtos, (int) page.getTotal(), page.getPageNum());
        return paged;
    }
    /**
     * 功能描述:查询所有数据
     *
     * @param exportParam
     * @return
     */
    public List<DcDecLogDto> selectAll(DcDecLogParam exportParam) {
        DcDecLog dcDecLog = dcDecLogDtoMapper.toPo(exportParam);
        List<DcDecLogDto> dcDecLogDtos = new ArrayList<>();
        List<DcDecLog> dcDecLogs = dcDecLogMapper.getList(dcDecLog);
        if (CollectionUtils.isNotEmpty(dcDecLogs)) {
            dcDecLogDtos = dcDecLogs.stream().map(item -> {
                DcDecLogDto dto = dcDecLogDtoMapper.toDto(item);
                return dto;
            }).collect(Collectors.toList());
        }
        return dcDecLogDtos;
    }
    /**
     * 功能描述:新增
     *
     * @param model
     * @param userInfo
     * @return
     */
    public DcDecLogDto insert(DcDecLogParam model, UserInfoToken userInfo) {
        String sid = UUID.randomUUID().toString();
        DcDecLog dcDecLog = dcDecLogDtoMapper.toPo(model);
        dcDecLog.setSid(sid);
        dcDecLog.setInsertUser(userInfo.getUserNo());
        dcDecLog.setInsertTime(new Date());
        int insertStatus = dcDecLogMapper.insert(dcDecLog);
       return insertStatus > 0 ? dcDecLogDtoMapper.toDto(dcDecLog) : null;
    }
    /**
     * 功能描述:修改
     *
     * @param dcDecLogParam
     * @param userInfo
     * @return
     */
    public DcDecLogDto update(DcDecLogParam dcDecLogParam, UserInfoToken userInfo) {
        DcDecLog dcDecLog = dcDecLogMapper.selectByPrimaryKey(dcDecLogParam.getSid());
        dcDecLogDtoMapper.updatePo(dcDecLogParam, dcDecLog);
        //dcDecLog.setUpdateUser(userInfo.getUserNo());
        dcDecLog.setUpdateTime(new Date());
        // 更新数据
        int update = dcDecLogMapper.updateByPrimaryKey(dcDecLog);
         return update > 0 ? dcDecLogDtoMapper.toDto(dcDecLog) : null;
    }
    /**
     * 功能描述:批量删除
     *
     * @param sids
     * @return
     */
    public void delete(List<String> sids, UserInfoToken userInfo) {
        dcDecLogMapper.deleteBySids(sids);
    }
	public int getListNumByHeadIds(List<String> sids) {
        return dcDecLogMapper.getListNumByHeadIds(sids);
    }
}
